package com.gkj.blog.controller;

import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.dto.DataTablesDto;
import com.gkj.blog.business.dto.PageResult;
import com.gkj.blog.business.enums.ArticleStatusEnum;
import com.gkj.blog.business.enums.ResponseStatus;
import com.gkj.blog.business.service.ArticleLookService;
import com.gkj.blog.business.service.ArticleService;
import com.gkj.blog.business.service.ArticleTagsService;
import com.gkj.blog.business.service.TypeService;
import com.gkj.blog.business.vo.condition.ArticleConditionVO;
import com.gkj.blog.business.vo.entity.ArticleLookVo;
import com.gkj.blog.business.vo.entity.ArticleVo;
import com.gkj.blog.framework.holder.RequestHolder;
import com.gkj.blog.framework.object.ResponseVO;
import com.gkj.blog.persistence.po.ArticleLook;
import com.gkj.blog.util.IpUtil;
import com.gkj.blog.util.RedisUtil;
import com.gkj.blog.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleLookService articleLookService;
    @Autowired
    TypeService typeService;
    @Autowired
    RedisUtil redisUtil;
    /**
     * 根据条件筛选
     * @param vo
     * @param tagList
     * @return
     */
    @PostMapping("/list")
    public PageResult list(ArticleConditionVO vo,@RequestParam(value="tagList[]",required = false) List<Long> tagList) {
        vo.setTagIds(tagList);
        vo.setStatus(ArticleStatusEnum.PUBLISHED);
        PageInfo<ArticleVo> pageInfo = articleService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @PostMapping("/lifeList")
    public PageResult lifeList(ArticleConditionVO vo) {
        vo.setTypeId(typeService.getByTypeName("生活随笔").getId());
        vo.setStatus(ArticleStatusEnum.PUBLISHED);
        PageInfo<ArticleVo> pageInfo = articleService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    /**
     * 最近文章
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/recent")
    public PageResult list(Integer pageNum,Integer pageSize) {
        PageInfo<ArticleVo> pageInfo = articleService.listRecent(pageNum,pageSize);
        return ResultUtil.tablePage(pageInfo);
    }


    /**
     * 热门文章
     * @param number
     * @return
     */
    @PostMapping("/hot")
    public ResponseVO getHostArticle(Integer number){
        if(number==null||number<=0)
            return ResultUtil.error(ResponseStatus.ERROR);
        List<ArticleVo> hostArticles=articleService.listHotArticle(number);
        return ResultUtil.success(null,hostArticles);
    }

    /**
     * 随机文章
     * @param number
     * @return
     */
    @PostMapping("/random")
    public ResponseVO getRandomArticle(Integer number){
        if(number==null||number<=0)
            return ResultUtil.error(ResponseStatus.ERROR);
        List<ArticleVo> randomArticles=articleService.listRandom(number);
        return ResultUtil.success(null,randomArticles);
    }

    /**
     * 推荐文章
     * @param number
     * @return
     */
    @PostMapping("/recommended")
    public ResponseVO getRecommendedArticle(Integer number){
        if(number==null||number<=0)
            return ResultUtil.error(ResponseStatus.ERROR);
        List<ArticleVo> recommendedArticles=articleService.listRecommended(number);
        return ResultUtil.success(null,recommendedArticles);
    }

    /**
     * 获取上一篇和下一篇文章
     * @param id
     * @return
     */
    @PostMapping("/getPrevAndNext/{id}")
    public ResponseVO getPreAndNext(@PathVariable Long id){
        Map preAndNext=articleService.getPrevAndNextArticles(id);
        if(preAndNext==null){
            return ResultUtil.error("未找到上下篇文章");
        }
        return ResultUtil.success(null,preAndNext);
    }

    /**
     * 获取相关文章
     * @param id
     * @param number
     * @return
     */
    @PostMapping("/getRelated/{id}")
    public ResponseVO getRelated(@PathVariable Long id,Integer number){
        List<ArticleVo> relatedArticleVos = articleService.listRelatedArticle(number, id);
        return ResultUtil.success(null,relatedArticleVos);
    }

    @PostMapping("/doPraise/{id}")
    public ResponseVO doPraise(@PathVariable Long id){
        try{
            articleService.doPraise(id);
        }catch (Exception e){
            return ResultUtil.error(e.getMessage());
        }
        return ResultUtil.success("点赞成功");
    }

}
